import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/authorization/information_page_controller.dart';
import 'package:kkhousekeep/widget/custom_app_bar.dart';
import 'package:kkhousekeep/widget/custom_button.dart';
import 'package:kkhousekeep/widget/form_widget.dart';

class BuildInformationPage extends StatelessWidget {
  const BuildInformationPage({
    super.key,
    required this.communityId,
    required this.roomId,
  });

  final int communityId;
  final int roomId;

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init:
            InformationPageController(communityId: communityId, roomId: roomId),
        builder: (controller) => GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
              child: Scaffold(
                resizeToAvoidBottomInset: false,
                appBar: CustomAppBar("填写业主信息"),
                body: ConstrainedBox(
                  constraints: const BoxConstraints.expand(),
                  child: Column(
                    children: [
                      _buildForm(controller),
                      SizedBox(height: 112.h),
                      _buildButton(controller)
                    ],
                  ),
                ),
              ),
            ));
  }

  ///表单组件
  Widget _buildForm(InformationPageController controller) {
    return Container(
        color: const Color(0xFFF4F5F6),
        child: Column(
          children: [
            SizedBox(
              height: 22.h,
            ),
            Obx(() => Container(
                  color: Colors.white,
                  padding: EdgeInsets.only(left: 32.w, right: 48.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      FormWidget.edit(
                        labelText: "业主姓名",
                        hintText: "请输入真实姓名",
                        onChange: (name) => controller.updateInformation(
                            name, OwnerInformationType.name),
                      ),
                      FormWidget.text(
                        labelText: "电话",
                        text: controller.phone.value,
                      ),
                      FormWidget.customSelect(
                        labelText: "证件类型",
                        selectText:
                            controller.certificateType.value.description,
                        onSelectClick: () =>
                            controller.buildCertificateWidget(),
                      ),
                      FormWidget.edit(
                        labelText: "证件号码",
                        hintText: "请输入证件号码",
                        onChange: (value) => controller.updateInformation(value, OwnerInformationType.certificateNumber),
                        inputFormatters: [LengthLimitingTextInputFormatter(18)],
                      ),
                      _buildPhoneForm(controller),
                      FormWidget.customSelect(
                          labelText: "入户日期",
                          unSelectText: "请选择入户日期",
                          selectText: controller.entryDate.value,
                          onSelectClick: () => controller.buildDatePick()),
                    ],
                  ),
                ))
          ],
        ));
  }

  ///按钮组件
  Widget _buildButton(InformationPageController controller) {
    return Obx(() => Align(
          alignment: Alignment.center,
          child: CustomButton(
            isAsh: controller.isAsh.value,
            width: 420.w,
            height: 90.h,
            text: "提交认证信息",
            onPressed: () => controller.submitInformation(),
          ),
        ));
  }

  ///上传列表子项
  Widget _buildPhoneForm(InformationPageController controller) {
    return Container(
      decoration: BoxDecoration(
          border: Border(
              bottom: BorderSide(width: 2.h, color: const Color(0XFFF0F2F7)))),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            height: 96.h,
            child: InkWell(
              onTap: () => controller.buildTakePhotoScreen(),
              child: Row(
                children: [
                  SizedBox(
                    width: 188.w,
                    child: Text(
                      "个人照片",
                      style: TextStyle(
                          fontSize: 32.sp, color: const Color(0xFF6D6D6D)),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Container(),
                    // child: ,
                  ),
                  Icon(
                    Icons.navigate_next,
                    color: const Color(0XFFCECECE),
                    size: 48.w,
                  ),
                ],
              ),
            ),
          ),
          controller.cacheImagePath.isNotEmpty
              ? InkWell(
                  onTap: () => controller.buildShowPhotoScreen(),
                  child: Column(
                    children: [
                      Container(
                        width: 200.h,
                        height: 280.h,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(
                            Radius.circular(16.w),
                          ),
                          image: DecorationImage(
                              image: FileImage(
                                File(controller.cacheImagePath.value),
                              ),
                              fit: BoxFit.fitWidth),
                        ),
                      ),
                      SizedBox(
                        height: 30.h,
                      )
                    ],
                  ),
                )
              : Container(),
        ],
      ),
    );
  }
}
